﻿using System;
using static H.Win32.NativeMethods;

namespace H.Win32
{
    /// <summary>
    /// 安全句柄
    /// </summary>
    internal struct SafeNativeHandle : IDisposable
    {
        private IntPtr _handle;

        private bool _isDisposed;

        public bool IsValid => _handle != IntPtr.Zero;

        public static implicit operator SafeNativeHandle(IntPtr value)
        {
            return new SafeNativeHandle() { _handle = value };
        }

        public static implicit operator IntPtr(SafeNativeHandle value)
        {
            return value._handle;
        }

        public void Dispose()
        {
            if (_isDisposed)
            {
                return;
            }

            if (_handle != CURRENT_PROCESS)
            {
                CloseHandle(_handle);
            }

            _isDisposed = true;
        }
    }
}
